package com.ys.peony.ui.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.ccrv.cber.sort.ItemData;
import com.ccrv.cber.sort.ItemDataWidthId;
import com.ys.peony.R;
import com.ys.peony.app.HttpURL;
import com.ys.peony.app.YSConstant;
import com.ys.peony.base.BaseActivity;
import com.ys.peony.car300.ListSelectActivity;
import com.ys.peony.enums.SwipeDirect;
import com.ys.peony.http.JCHttpManagerUtils;
import com.ys.peony.http.base.BaseRespBean;
import com.ys.peony.http.base.BaseRespCallBack;
import com.ys.peony.model.AddLoanClientResp;
import com.ys.peony.model.BusiContactCoModel;
import com.ys.peony.model.BusiLoanModel;
import com.ys.peony.model.ClientInfoModel;
import com.ys.peony.model.YWDJPrepareAreaItemModel;
import com.ys.peony.model.YWDJPrepareItemModel;
import com.ys.peony.model.YWDJPrepareModel;
import com.ys.peony.ui.widget.ClientClassifyPicker;
import com.ys.peony.ui.widget.TypeValPicker;
import com.ys.peony.utils.CommonUtils;
import com.ys.peony.utils.JSONUtil;
import com.ys.peony.utils.LoadingProgressUtils;
import com.ys.peony.utils.LogUtil;
import com.ys.peony.utils.NetUtils;
import com.ys.peony.utils.PixelUtils;
import com.ys.peony.utils.SharePreferencesUtils;
import com.ys.peony.utils.StringUtils;
import com.ys.peony.utils.ToastUtils;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;

import static com.ys.peony.app.YSConstant.ACTIVITY_AREACODE;

/**
 * 增加借款人信息
 * Created by yml on 17/5/27.
 */

public class AddClientInfoActivity extends BaseActivity implements View.OnClickListener,
        TypeValPicker.TypeValCallBack, ClientClassifyPicker.ClientClassifyCallBack {

    @BindView(R.id.rootview)
    RelativeLayout rootView;

    @BindView(R.id.back_btn)
    RelativeLayout backBtn;

    @BindView(R.id.title_name_tv)
    TextView titleNameTv;

    @BindView(R.id.commit_btn)
    Button commitBtn;

    @BindView(R.id.name_et)
    EditText nameET;
    @BindView(R.id.idcard_et)
    EditText idCardET;
    @BindView(R.id.phone_et)
    EditText phoneET;
    @BindView(R.id.company_et)
    EditText companyET;
    @BindView(R.id.spouse_name_et)
    EditText spouseNameET;
    @BindView(R.id.spouse_idcard_et)
    EditText spouseIdCardET;
    @BindView(R.id.spouse_phone_et)
    EditText spousePhoneET;
    @BindView(R.id.applyamount_et)
    EditText applyAmountET;

    @BindView(R.id.sex_tv)
    TextView sexTV;
    @BindView(R.id.marriage_tv)
    TextView marriageTV;
    @BindView(R.id.registry_tv)
    TextView domicileAreaTV;
    @BindView(R.id.unit_character_tv)
    TextView companyTypeTV;
    @BindView(R.id.industry_tv)
    TextView industryTV;
    @BindView(R.id.job_tv)
    TextView dutyTV;
    @BindView(R.id.hasproperty_tv)
    TextView hasPropertyRightTV;
    @BindView(R.id.propertystatus_tv)
    TextView propertyRightStatusTV;
    @BindView(R.id.gongjie_tv)
    TextView hasContactsTypeCoTV;
    @BindView(R.id.express_client_tv)
    TextView clientClassIdTV;
    @BindView(R.id.islocalhouse_tv)
    TextView isLocalHouseTV;

    @BindView(R.id.sex_rl)
    RelativeLayout sexRL;
    @BindView(R.id.marriage_rl)
    RelativeLayout marriageRL;
    @BindView(R.id.registry_rl)
    RelativeLayout domicileAreaRL;
    @BindView(R.id.unit_character_rl)
    RelativeLayout companyTypeRL;
    @BindView(R.id.industry_rl)
    RelativeLayout industryRL;
    @BindView(R.id.job_rl)
    RelativeLayout dutyRL;
    @BindView(R.id.hasproperty_rl)
    RelativeLayout hasPropertyRightRL;
    @BindView(R.id.propertystatus_rl)
    RelativeLayout propertyRightStatusRL;
    @BindView(R.id.gongjie_rl)
    RelativeLayout hasContactsTypeCoRL;
    @BindView(R.id.express_client_rl)
    RelativeLayout clientClassIdRL;
    @BindView(R.id.islocalhouse_rl)
    RelativeLayout isLocalHouseRL;

    @BindView(R.id.spouse_name_ll)
    LinearLayout spouseNameLL;
    @BindView(R.id.spouse_phone_ll)
    LinearLayout spousePhoneLL;
    @BindView(R.id.spouse_identification_ll)
    LinearLayout spouseIdCardLL;

    private YWDJPrepareModel prepareModel;
    private TypeValPicker typeValPicker;
    //    private AreaPicker areaPicker;
    private ClientClassifyPicker clientClassifyPicker;

    private int popWidth;
    private List<YWDJPrepareItemModel> isTotalLoanList = new ArrayList<YWDJPrepareItemModel>();
    private String sexVal;
    private String marriageVal;
    private int domicileAreaVal;
    private String companyTypeVal;
    private String industryVal;
    private String dutyVal;
    private String hasPropertyRightVal;
    private String propertyRightStatusVal;
    private String hasContactsTypeCoVal;
    private String isLocalHouseVal;
    private String clientClassIdVal;

    private boolean addFlag = false;
    private String loanId;

    private BusiLoanModel busiLoanModel;
    private ClientInfoModel clientInfoModel;
    private BusiContactCoModel busiContactCo;

    private List<YWDJPrepareItemModel> sexList;
    private List<YWDJPrepareItemModel> marriageList;
    private List<YWDJPrepareItemModel> hasPropertyList;
    private List<YWDJPrepareItemModel> propertyStatusList;
    private List<YWDJPrepareItemModel> dutyList;
    private List<YWDJPrepareItemModel> industryList;
    private List<YWDJPrepareItemModel> companyTypeList;
    private List<YWDJPrepareItemModel> localHouseList;

    private ItemDataWidthId mItemDataWidthId = null;

    @Override
    protected int getContentResId() {
        return R.layout.activity_add_jiekuanren_info;
    }

    @Override
    protected void initView() {
        WindowManager wm = (WindowManager) getSystemService(Context.WINDOW_SERVICE);

        int width = wm.getDefaultDisplay().getWidth();
        popWidth = (width - PixelUtils.dip2px(40, this)) * 2 / 3;
        LogUtil.i("aaaa", "*******popWidth****" + popWidth);
        backBtn.setOnClickListener(this);
        commitBtn.setOnClickListener(this);

        sexRL.setOnClickListener(this);
        marriageRL.setOnClickListener(this);
        domicileAreaRL.setOnClickListener(this);
        companyTypeRL.setOnClickListener(this);
        industryRL.setOnClickListener(this);
        dutyRL.setOnClickListener(this);
        hasPropertyRightRL.setOnClickListener(this);
        propertyRightStatusRL.setOnClickListener(this);
        hasContactsTypeCoRL.setOnClickListener(this);
        clientClassIdRL.setOnClickListener(this);
        isLocalHouseRL.setOnClickListener(this);

        setIsTotalLoanList();//是否有共借

        Bundle bd = getIntent().getExtras();
        if (bd != null) {
            loanId = bd.getString("loanId");
            addFlag = bd.getBoolean("addFlag");
        }

        if (addFlag) {
            getYwdjPrepareList();
            titleNameTv.setText(getResources().getString(R.string.ywdj_add_title));
        } else {
            getBusiInfoModel();
            commitBtn.setText(getResources().getString(R.string.save_tv));
            titleNameTv.setText(getResources().getString(R.string.ywdj_detail_title));

        }
    }

    @Override
    public void onClick(View v) {
        closeKeyboard();
        switch (v.getId()) {
            case R.id.commit_btn:
                commit();
                break;
            case R.id.sex_rl:
                if (prepareModel == null || prepareModel.getClientSexList() == null || prepareModel.getClientSexList().size() <= 0) {
                    ToastUtils.showToast(AddClientInfoActivity.this, "初始化数据失败");
                    return;
                }
                if (typeValPicker == null) {
                    typeValPicker = new TypeValPicker(this, this);
                }
//                typeValPicker.show(sexRL, prepareModel.getClientSexList(), YSConstant.POP_TYPE_SEX);
                typeValPicker.show(rootView, prepareModel.getClientSexList(), YSConstant.POP_TYPE_SEX);
                break;
            case R.id.marriage_rl:
                if (prepareModel == null || prepareModel.getClientMarriageList() == null || prepareModel.getClientMarriageList().size() <= 0) {
                    ToastUtils.showToast(AddClientInfoActivity.this, "初始化数据失败");
                    return;
                }
                if (typeValPicker == null) {
                    typeValPicker = new TypeValPicker(this, this);
                }
//                typeValPicker.show(marriageRL, prepareModel.getClientMarriageList(), YSConstant.POP_TYPE_MARRIGE);
                typeValPicker.show(rootView, prepareModel.getClientMarriageList(), YSConstant.POP_TYPE_MARRIGE);
                break;
            case R.id.registry_rl:
                if (prepareModel == null || prepareModel.getAreaList() == null || prepareModel.getAreaList().size() <= 0) {
                    ToastUtils.showToast(AddClientInfoActivity.this, "初始化数据失败");
                    return;
                }
                if(mItemDataWidthId == null) {
                    LoadingProgressUtils.showProgressDialog(this);
                    mItemDataWidthId = initData();
                    LoadingProgressUtils.closeDialog();
                }
                if (mItemDataWidthId != null && mItemDataWidthId.getItemDatas().size() > 0) {
                    Bundle bundle = new Bundle();
                    bundle.putSerializable(ListSelectActivity.ITEM_PARAM_WIDTH_ID, mItemDataWidthId);
                    skipToTargetPage(ListSelectActivity.class, bundle, SwipeDirect.DIRECT_LEFT, true, ACTIVITY_AREACODE);
                }
                break;
            case R.id.unit_character_rl:
                if (prepareModel == null || prepareModel.getClientCompanyTypeList() == null || prepareModel.getClientCompanyTypeList().size() <= 0) {
                    ToastUtils.showToast(AddClientInfoActivity.this, "初始化数据失败");
                    return;
                }
                if (typeValPicker == null) {
                    typeValPicker = new TypeValPicker(this, this);
                }
//                typeValPicker.show(companyTypeRL, prepareModel.getClientCompanyTypeList(), YSConstant.POP_TYPE_COMPANYTYPE);
                typeValPicker.show(rootView, prepareModel.getClientCompanyTypeList(), YSConstant.POP_TYPE_COMPANYTYPE);
                break;
            case R.id.industry_rl:
                if (prepareModel == null || prepareModel.getClientIndustryList() == null || prepareModel.getClientIndustryList().size() <= 0) {
                    ToastUtils.showToast(AddClientInfoActivity.this, "初始化数据失败");
                    return;
                }
                if (typeValPicker == null) {
                    typeValPicker = new TypeValPicker(this, this);
                }
//                typeValPicker.show(industryRL, prepareModel.getClientIndustryList(), YSConstant.POP_TYPE_INDUSTRY);
                typeValPicker.show(rootView, prepareModel.getClientIndustryList(), YSConstant.POP_TYPE_INDUSTRY);
                break;
            case R.id.job_rl:
                if (prepareModel == null || prepareModel.getClientDutyList() == null || prepareModel.getClientDutyList().size() <= 0) {
                    ToastUtils.showToast(AddClientInfoActivity.this, "初始化数据失败");
                    return;
                }
                if (typeValPicker == null) {
                    typeValPicker = new TypeValPicker(this, this);
                }
//                typeValPicker.show(dutyRL, prepareModel.getClientDutyList(), YSConstant.POP_TYPE_DUTY);
                typeValPicker.show(rootView, prepareModel.getClientDutyList(), YSConstant.POP_TYPE_DUTY);
                break;
            case R.id.hasproperty_rl:
                if (prepareModel == null || prepareModel.getHavePropertyRight() == null || prepareModel.getHavePropertyRight().size() <= 0) {
                    ToastUtils.showToast(AddClientInfoActivity.this, "初始化数据失败");
                    return;
                }
                if (typeValPicker == null) {
                    typeValPicker = new TypeValPicker(this, this);
                }
//                typeValPicker.show(hasPropertyRightRL, prepareModel.getHavePropertyRight(), YSConstant.POP_TYPE_HASPROPERTY);
                typeValPicker.show(rootView, prepareModel.getHavePropertyRight(), YSConstant.POP_TYPE_HASPROPERTY);
                break;
            case R.id.islocalhouse_rl:
                if (prepareModel == null || prepareModel.getLocalHouseList() == null || prepareModel.getLocalHouseList().size() <= 0) {
                    ToastUtils.showToast(AddClientInfoActivity.this, "初始化数据失败");
                    return;
                }
                if (typeValPicker == null) {
                    typeValPicker = new TypeValPicker(this, this);
                }
//                typeValPicker.show(isLocalHouseRL, prepareModel.getLocalHouseList(), YSConstant.POP_TYPE_ISLOCALHOUSE);
                typeValPicker.show(rootView, prepareModel.getLocalHouseList(), YSConstant.POP_TYPE_ISLOCALHOUSE);
                break;
            case R.id.propertystatus_rl:
                if (prepareModel == null || prepareModel.getClientPropertyRightStatusList() == null || prepareModel.getClientPropertyRightStatusList().size() <= 0) {
                    ToastUtils.showToast(AddClientInfoActivity.this, "初始化数据失败");
                    return;
                }
                if (typeValPicker == null) {
                    typeValPicker = new TypeValPicker(this, this);
                }
//                typeValPicker.show(propertyRightStatusRL, prepareModel.getClientPropertyRightStatusList(), YSConstant.POP_TYPE_PROPERTYSTATUS);
                typeValPicker.show(rootView, prepareModel.getClientPropertyRightStatusList(), YSConstant.POP_TYPE_PROPERTYSTATUS);
                break;
            case R.id.gongjie_rl:
                if (typeValPicker == null) {
                    typeValPicker = new TypeValPicker(this, this);
                }
//                typeValPicker.show(hasContactsTypeCoRL, isTotalLoanList, YSConstant.POP_TYPE_ISTOTALLOAN);
                typeValPicker.show(rootView, isTotalLoanList, YSConstant.POP_TYPE_ISTOTALLOAN);
                break;
            case R.id.express_client_rl:
                if (prepareModel == null || prepareModel.getSpecialClientClassifyList() == null || prepareModel.getSpecialClientClassifyList().size() <= 0) {
                    ToastUtils.showToast(AddClientInfoActivity.this, "初始化数据失败");
                    return;
                }
                if (clientClassifyPicker == null) {
                    clientClassifyPicker = new ClientClassifyPicker(this, this);
                }
                clientClassifyPicker.show(rootView, prepareModel.getSpecialClientClassifyList());
                break;
            case R.id.back_btn:
                finish();
                break;
        }
    }

    /**
     * 提交用户信息
     */
    private void commit() {
        if (!NetUtils.isNetworkConnected(this)) {
            ToastUtils.showToast(this, getResources().getString(R.string.no_net_error));
            return;
        }

        if (StringUtils.isEmpty(nameET.getText().toString())) {
            ToastUtils.showToast(this, "请填写姓名");
            return;
        }
        if (StringUtils.isEmpty(idCardET.getText().toString())) {
            ToastUtils.showToast(this, "请填写身份证号");
            return;
        }
        String idCardStr = idCardET.getText().toString();
        if (!CommonUtils.isIdentityCard(idCardStr)) {
            ToastUtils.showToast(this, "请填写正确的身份证号");
            return;
        }
        if (StringUtils.isEmpty(phoneET.getText().toString())) {
            ToastUtils.showToast(this, "请填写手机号");
            return;
        }

        if (StringUtils.isEmpty(companyET.getText().toString())) {
            ToastUtils.showToast(this, "请填写工作单位");
            return;
        }

        if ("1".equals(hasContactsTypeCoVal)) {
            if (StringUtils.isEmpty(spouseNameET.getText().toString())) {
                ToastUtils.showToast(this, "请填写配偶姓名");
                return;
            }
            if (StringUtils.isEmpty(spousePhoneET.getText().toString())) {
                ToastUtils.showToast(this, "请填写配偶手机号");
                return;
            }

            if (StringUtils.isEmpty(spouseIdCardET.getText().toString())) {
                ToastUtils.showToast(this, "请填写配偶证件号");
                return;
            }
            String idCardStr2 = spouseIdCardET.getText().toString();
            if (!CommonUtils.isIdentityCard(idCardStr2)) {
                ToastUtils.showToast(this, "请填写配偶正确的身份证号");
                return;
            }
        }

        if (StringUtils.isEmpty(sexVal)) {
            ToastUtils.showToast(this, "请选择性别");
            return;
        }
        if (StringUtils.isEmpty(marriageVal)) {
            ToastUtils.showToast(this, "请选择婚姻状况");
            return;
        }
        if (domicileAreaVal <= 0) {
            ToastUtils.showToast(this, "请选择户籍");
            return;
        }
        if (StringUtils.isEmpty(companyTypeVal)) {
            ToastUtils.showToast(this, "请选择单位性质");
            return;
        }
        if (StringUtils.isEmpty(industryVal)) {
            ToastUtils.showToast(this, "请选择行业");
            return;
        }
        if (StringUtils.isEmpty(dutyVal)) {
            ToastUtils.showToast(this, "请选择职务");
            return;
        }
        if (StringUtils.isEmpty(isLocalHouseVal)) {
            ToastUtils.showToast(this, "请选择本地房产");
            return;
        }
        if (StringUtils.isEmpty(hasPropertyRightVal)) {
            ToastUtils.showToast(this, "请选择是否有产权");
            return;
        }
        if (StringUtils.isEmpty(propertyRightStatusVal)) {
            ToastUtils.showToast(this, "请选择产权状态");
            return;
        }
        if (StringUtils.isEmpty(applyAmountET.getText().toString())) {
            ToastUtils.showToast(this, "请填写申请金额");
            return;
        }
        LoadingProgressUtils.showProgressDialog(this);
        JCHttpManagerUtils httpManager = new JCHttpManagerUtils();
        Map<String, Object> params = new HashMap<String, Object>();
        // val id
        params.put("userId", Long.parseLong(SharePreferencesUtils.getInstance().getUserModel().getId()));//(必传) Long  用户主键

        if (!addFlag) {
            params.put("busiLoan.id", loanId);//(必传) String 业务主键
            params.put("busiClientMain.id", clientInfoModel.getId());//(必传) String 客户主键
            if (busiContactCo != null) {
                params.put("busiContactCo.id", StringUtils.trimNull(busiContactCo.getId() + ""));//(必传) String 共借人主键
            }
        }
        params.put("busiLoan.apply_amount", applyAmountET.getText().toString());// (必传) String 申请金额
        params.put("busiClientMain.name", nameET.getText().toString());//(必传) String 姓名
        params.put("busiClientMain.id_card", idCardET.getText().toString());//(必传) String 身份证
        params.put("busiClientMain.sex", sexVal);//(必传) String  0 男 1 女 性别
        params.put("busiClientMain.marriage", marriageVal);//(必传) String 婚姻状况
        params.put("busiClientMain.domicile_area_id", domicileAreaVal);//(必传) Long 户籍
        params.put("busiClientMain.mobile", phoneET.getText().toString());//(必传) String 手机号
        params.put("busiClientMain.industry", industryVal);//(必传) String 行业
        params.put("busiClientMain.company", companyET.getText().toString());//(必传) String 工作单位
        params.put("busiClientMain.duty", dutyVal);//(职务) String 职务
        params.put("busiClientMain.company_type", companyTypeVal);//(必传) String 单位性质
        params.put("busiClientMain.is_local_house", isLocalHouseVal);//(必传) String 本地房产
        params.put("hasContactsTypeCo", hasContactsTypeCoVal);//(非必传) String 是否共借人，不是共借人：不传这个字段 是共借人：任意传值
        if ("1".equals(hasContactsTypeCoVal)) {
            params.put("busiContactCo.name", spouseNameET.getText().toString());//(非必传) String 家庭信息-姓名，是共借人必传
            params.put("busiContactCo.tel", spousePhoneET.getText().toString());//(非必传) String 家庭信息-手机号，是共借人必传
            params.put("busiContactCo.id_card", spouseIdCardET.getText().toString());//(非必传) String 家庭信息-证件号，是共借人必传

        }
        params.put("busiClientMain.client_clas_id", clientClassIdVal);//(非必传) Long 特殊客户类别，选中c1，c2时必传
        params.put("busiClientMain.have_property_right", hasPropertyRightVal);//(必传) String 是否有产权
        params.put("busiClientMain.property_right_status", propertyRightStatusVal);//(必传) String 产权状态
        if (!addFlag) {
            params.put("busiClientMain.bank", "bank");//(非必传) String 开户行
            params.put("busiClientMain.account_city", 15);//(非必传) Long 开户城市
            params.put("busiClientMain.branch_bank", "branchbank");//(非必传) String 支行
            params.put("busiClientMain.card_number", "360727198411079435");//(非必传) String 卡号
            params.put("busiClientMain.is_local_house", "1");//0表示否，1表示是
        }
        params.put("busiLoan.sales_manager_name",SharePreferencesUtils.getInstance().getUserModel().getName());
        params.put("busiLoan.sales_manager",SharePreferencesUtils.getInstance().getUserId());
        LogUtil.i("aaaa", "*****params:" + params.toString());
        if (addFlag) {
            httpManager.get(HttpURL.ywdjAdd, params, new BaseRespCallBack(AddClientInfoActivity.this) {

                public void onRespSuccess(String respStr) {
                    LogUtil.i("aaaa", "___list_" + respStr);
                    final AddLoanClientResp addLoanClientResp = JSONUtil.parseObject(respStr, AddLoanClientResp.class);
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            ToastUtils.showToast(AddClientInfoActivity.this, "添加成功");
                            finish();
                        }
                    });
                }

                @Override
                public void onRespFailure(String msg) {
                    super.onRespFailure(msg);
                    ToastUtils.showToast(AddClientInfoActivity.this, msg);
                }
            });
        } else {
            httpManager.get(HttpURL.ywdjUpdate, params, new BaseRespCallBack(AddClientInfoActivity.this) {

                public void onRespSuccess(String respStr) {
                    LogUtil.i("aaaa", "___list_" + respStr);
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            ToastUtils.showToast(AddClientInfoActivity.this, "修改成功");
                            finish();
                        }
                    });
                }

                @Override
                public void onRespFailure(String msg) {
                    super.onRespFailure(msg);
                    ToastUtils.showToast(AddClientInfoActivity.this, msg);
                }
            });
        }
    }

    private void getYwdjPrepareList() {
        LoadingProgressUtils.showProgressDialog(this);
        JCHttpManagerUtils httpManager = new JCHttpManagerUtils();
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("userId", SharePreferencesUtils.getInstance().getUserId());
        httpManager.get(HttpURL.ywdjPrepare, params, new BaseRespCallBack(AddClientInfoActivity.this) {

            public void onRespSuccess(String respStr) {
                LogUtil.i("aaaa", "___getYwdjPrepareList_" + respStr);
                //业务登记准备数据
                prepareModel = JSONUtil.parseObject(respStr, YWDJPrepareModel.class);
                try {
                    JSONObject jb = new JSONObject(respStr);
                    LogUtil.i("aaaa", "_______");
                    SharePreferencesUtils.getInstance().setKeyValue(YSConstant.SP_CAR_BRANCH, jb.getJSONArray("brandList").toString());
                    SharePreferencesUtils.getInstance().setKeyValue(YSConstant.SP_CAR_OWNERSHOP, jb.getJSONArray("vehicleOwnershopTagList").toString());
                    JSONArray areaCodeArray = jb.getJSONArray("areaList");
                    JSONObject childJb = areaCodeArray.getJSONObject(0);
                    SharePreferencesUtils.getInstance().setKeyValue(YSConstant.SP_CAR_AREACODE, childJb.getJSONArray("sysAreaList").toString());
                    SharePreferencesUtils.getInstance().setKeyValue(YSConstant.SP_AREACODE, areaCodeArray.toString());

                } catch (JSONException e) {
                    e.printStackTrace();
                }

            }

            @Override
            public void onRespFailure(String msg) {
                super.onRespFailure(msg);
                ToastUtils.showToast(AddClientInfoActivity.this, msg);
            }
        });
    }

    @Override
    public void pickerTypeValItemOnclick(String text, String val, int type) {
        //根据type 赋值
        switch (type) {
            case YSConstant.POP_TYPE_SEX:
                sexTV.setText(text);
                sexVal = val;
                break;
            case YSConstant.POP_TYPE_MARRIGE:
                marriageTV.setText(text);
                marriageVal = val;
                if ("0".equals(val) || "3".equals(val)) {
                    setTotalLoanView("否", "0");
                }
                break;
            case YSConstant.POP_TYPE_COMPANYTYPE:
                companyTypeTV.setText(text);
                companyTypeVal = val;
                break;
            case YSConstant.POP_TYPE_INDUSTRY:
                industryTV.setText(text);
                industryVal = val;
                break;
            case YSConstant.POP_TYPE_DUTY:
                dutyTV.setText(text);
                dutyVal = val;
                break;
            case YSConstant.POP_TYPE_HASPROPERTY:
                hasPropertyRightTV.setText(text);
                hasPropertyRightVal = val;
                break;
            case YSConstant.POP_TYPE_ISLOCALHOUSE:
                isLocalHouseTV.setText(text);
                isLocalHouseVal = val;
                break;
            case YSConstant.POP_TYPE_PROPERTYSTATUS:
                propertyRightStatusTV.setText(text);
                propertyRightStatusVal = val;
                break;
            case YSConstant.POP_TYPE_ISTOTALLOAN:
                setTotalLoanView(text, val);
                break;
        }
    }

    private void setTotalLoanView(String text, String val) {
        hasContactsTypeCoTV.setText(text);
        hasContactsTypeCoVal = val;
        if ("0".equals(val)) {
            spouseNameLL.setVisibility(View.GONE);
            spousePhoneLL.setVisibility(View.GONE);
            spouseIdCardLL.setVisibility(View.GONE);
        } else {
            spouseNameLL.setVisibility(View.VISIBLE);
            spousePhoneLL.setVisibility(View.VISIBLE);
            spouseIdCardLL.setVisibility(View.VISIBLE);
        }
    }

    private void setIsTotalLoanList() {
        YWDJPrepareItemModel item = new YWDJPrepareItemModel();
        item.setVal("0");
        item.setText("否");
        YWDJPrepareItemModel item1 = new YWDJPrepareItemModel();
        item1.setVal("1");
        item1.setText("是");
        isTotalLoanList.add(item);
        isTotalLoanList.add(item1);
    }

    @Override
    public void clientClassifyItemOnclick(String text, long val) {
        clientClassIdTV.setText(text);
        clientClassIdVal = val + "";
    }

    /**
     * 业务登记查看
     */
    private void getBusiInfoModel() {
        if (!NetUtils.isNetworkConnected(this)) {
            ToastUtils.showToast(this, getResources().getString(R.string.no_net_error));
            return;
        }
        LoadingProgressUtils.showProgressDialog(this);
        JCHttpManagerUtils httpManager = new JCHttpManagerUtils();
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("userId", Long.parseLong(SharePreferencesUtils.getInstance().getUserModel().getId()));
        params.put("loanId", loanId);

        httpManager.get(HttpURL.ywdjSee, params, new BaseRespCallBack(AddClientInfoActivity.this) {

            public void onRespSuccess(String respStr) {
                LogUtil.i("aaaa", "___BusiInfoActivity_" + respStr);
                prepareModel = (YWDJPrepareModel) JSONUtil.parseObject(respStr, YWDJPrepareModel.class);
                sexList = prepareModel.getClientSexList();
                marriageList = prepareModel.getClientMarriageList();
                hasPropertyList = prepareModel.getHavePropertyRight();
                propertyStatusList = prepareModel.getClientPropertyRightStatusList();
                companyTypeList = prepareModel.getClientCompanyTypeList();
                localHouseList = prepareModel.getLocalHouseList();
                industryList = prepareModel.getClientIndustryList();
                dutyList = prepareModel.getClientDutyList();
                busiLoanModel = prepareModel.getBusiLoanVo();
                clientInfoModel = busiLoanModel.getBusiClientMain();
                try {
                    JSONObject jb = new JSONObject(respStr);
                    LogUtil.i("aaaa", "_______");
                    SharePreferencesUtils.getInstance().setKeyValue(YSConstant.SP_CAR_BRANCH, jb.getJSONArray("brandList").toString());
                    SharePreferencesUtils.getInstance().setKeyValue(YSConstant.SP_CAR_OWNERSHOP, jb.getJSONArray("vehicleOwnershopTagList").toString());
                    JSONArray areaCodeArray = jb.getJSONArray("areaList");
                    JSONObject childJb = areaCodeArray.getJSONObject(0);
                    SharePreferencesUtils.getInstance().setKeyValue(YSConstant.SP_CAR_AREACODE, childJb.getJSONArray("sysAreaList").toString());
                    SharePreferencesUtils.getInstance().setKeyValue(YSConstant.SP_AREACODE, areaCodeArray.toString());

                } catch (JSONException e) {
                    e.printStackTrace();
                }
//              申请金额  localHouseList
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        initBusiInfoView();
                    }
                });

            }

            @Override
            public void onRespFailure(String msg) {
                super.onRespFailure(msg);
                ToastUtils.showToast(AddClientInfoActivity.this, msg);
            }
        });
    }

    /**
     * 初始化业务登记界面
     */
    private void initBusiInfoView() {
        nameET.setText(clientInfoModel.getName());
        sexTV.setText(CommonUtils.getTextbyVal(clientInfoModel.getSex(), sexList));
        idCardET.setText(clientInfoModel.getId_card());
        marriageTV.setText(CommonUtils.getTextbyVal(clientInfoModel.getMarriage(), marriageList));
        phoneET.setText(clientInfoModel.getMobile());
        domicileAreaTV.setText(clientInfoModel.getFull_name());
        companyET.setText(clientInfoModel.getCompany());
        hasPropertyRightTV.setText(CommonUtils.getTextbyVal(clientInfoModel.getHave_property_right(), hasPropertyList));
        propertyRightStatusTV.setText(CommonUtils.getTextbyVal(clientInfoModel.getProperty_right_status(), propertyStatusList));
        clientClassIdTV.setText(clientInfoModel.getClient_clas_name());

        clientClassIdVal = clientInfoModel.getClient_clas_id() + "";
        propertyRightStatusVal = clientInfoModel.getProperty_right_status();
        marriageVal = clientInfoModel.getMarriage();
        hasPropertyRightVal = clientInfoModel.getHave_property_right();
        domicileAreaVal = clientInfoModel.getDomicile_area_id();
        sexVal = clientInfoModel.getSex();
        companyTypeTV.setText(CommonUtils.getTextbyVal(clientInfoModel.getCompany_type(), companyTypeList));
        isLocalHouseTV.setText(CommonUtils.getTextbyVal(clientInfoModel.getIs_local_house(), localHouseList));
        isLocalHouseVal = clientInfoModel.getIs_local_house();
        industryTV.setText(CommonUtils.getTextbyVal(clientInfoModel.getIndustry(), industryList));
        dutyTV.setText(CommonUtils.getTextbyVal(clientInfoModel.getDuty(), dutyList));
        companyTypeVal = clientInfoModel.getCompany_type();
        industryVal = clientInfoModel.getIndustry();
        dutyVal = clientInfoModel.getDuty();
        busiContactCo = busiLoanModel.getBusiContactCo();
        if ((busiContactCo != null && busiContactCo.getId() > 0)) {

            hasContactsTypeCoVal = "1";
            hasContactsTypeCoTV.setText("是");
            spouseNameET.setText(busiContactCo.getName());
            spousePhoneET.setText(busiContactCo.getTel());
            spouseIdCardET.setText(busiContactCo.getId_card());
            spouseNameLL.setVisibility(View.VISIBLE);
            spousePhoneLL.setVisibility(View.VISIBLE);
            spouseIdCardLL.setVisibility(View.VISIBLE);
        } else {
            hasContactsTypeCoVal = "0";
            hasContactsTypeCoTV.setText("否");
            spouseNameLL.setVisibility(View.GONE);
            spousePhoneLL.setVisibility(View.GONE);
            spouseIdCardLL.setVisibility(View.GONE);
        }
        try {
            applyAmountET.setText(prepareModel.getBusiLoanVo().getBusiLoan().getApply_amount() + "");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (ACTIVITY_AREACODE == requestCode && resultCode == RESULT_OK) {
            ItemData itemData = (ItemData) data.getSerializableExtra(ListSelectActivity.ITEM_PARAM);
            if (itemData != null) {
                String areaName = itemData.getCar_title();
                int areaId = (int) itemData.getId();
                domicileAreaTV.setText(areaName);
                domicileAreaVal = areaId;
            }
        }
    }

    private void closeKeyboard() {
        View view = getWindow().peekDecorView();
        if (view != null) {
            InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
            inputMethodManager.hideSoftInputFromWindow(view.getWindowToken(), 0);
        }
    }

    private ItemDataWidthId initData() {
        ItemDataWidthId itemDataWidthId = new ItemDataWidthId();
        List<ItemData> itemDatas = new ArrayList<>();
        List<YWDJPrepareAreaItemModel> ywdjPrepareAreaItemModelList = prepareModel.getAreaList().get(0).getSysAreaList();
        if (ywdjPrepareAreaItemModelList == null || ywdjPrepareAreaItemModelList.size() <= 0) {
            return null;
        }
        for (int i = 0; i < ywdjPrepareAreaItemModelList.size(); i++) {
            List<YWDJPrepareAreaItemModel> ywdjPrepareAreaItemModelList2 = ywdjPrepareAreaItemModelList.get(i).getSysAreaList();
            if (ywdjPrepareAreaItemModelList2 == null || ywdjPrepareAreaItemModelList2.size() <= 0) {
                ItemData itemData = new ItemData();
                String name = ywdjPrepareAreaItemModelList.get(i).getName();
                long id = ywdjPrepareAreaItemModelList.get(i).getId();
                itemData.setId(id);
                itemData.setCar_title(name);
                itemDatas.add(itemData);
                continue;
            }
            for (int j = 0; j < ywdjPrepareAreaItemModelList2.size(); j++) {
                List<YWDJPrepareAreaItemModel> ywdjPrepareAreaItemModelList3 = ywdjPrepareAreaItemModelList2.get(j).getSysAreaList();
                if (ywdjPrepareAreaItemModelList3 == null || ywdjPrepareAreaItemModelList3.size() <= 0) {
                    ItemData itemData = new ItemData();
                    String name = ywdjPrepareAreaItemModelList.get(i).getName() + "-" + ywdjPrepareAreaItemModelList2.get(j).getName();
                    long id = ywdjPrepareAreaItemModelList2.get(j).getId();
                    itemData.setId(id);
                    itemData.setCar_title(name);
                    itemDatas.add(itemData);
                    continue;
                }
                for (int k = 0; k < ywdjPrepareAreaItemModelList3.size(); k++) {
                    ItemData itemData = new ItemData();
                    String name = ywdjPrepareAreaItemModelList.get(i).getName()
                            + "-" + ywdjPrepareAreaItemModelList2.get(j).getName()
                            + "-" + ywdjPrepareAreaItemModelList3.get(k).getName();
                    long id = ywdjPrepareAreaItemModelList3.get(k).getId();
                    itemData.setId(id);
                    itemData.setCar_title(name);
                    itemDatas.add(itemData);
                }
            }
        }
        itemDataWidthId.setItemDatas(itemDatas);
        return itemDataWidthId;
    }
}
